package com.shensi.appCollect.utils;

import com.google.gson.Gson;

public class ResultData<T> {
    private String message;
    private Integer retCode;
    private T data;
    private static Gson gson = new Gson();

    private ResultData(T data) {
        this.retCode = 0;
        this.message = "成功";
        this.data = data;
    }

    private ResultData(String  msg,T data) {
        this.retCode = 0;
        this.message = msg;
        this.data = data;
    }
    private ResultData(Integer retCode, String  msg) {
        this.retCode = retCode;
        this.message = msg;

        if(this.retCode == null){
            this.retCode = -1;
        }

        if(StringUtils.isBlank(this.message)){
            this.message = "";
        }
    }
    private ResultData(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.retCode = cm.getRetCode();
        this.message = cm.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> ResultData<T> success(T data){
        return new ResultData<T>(data);
    }

    public static <T> ResultData<T> success(String  msg,T data){
        return new ResultData<T>(msg, data);
    }

    /**
     * 成功时候的调用
     * @return
     */
    public static <T>  String successJson(T data){

        return gson.toJson(new ResultData<T>(data));
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> ResultData<T> success(){
        return (ResultData<T>) success("");
    }

    public static String successJson(String msg){
        return gson.toJson(new ResultData(0,msg));
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> ResultData<T> error(CodeMsg cm){
        return new ResultData<T>(cm);
    }

    public static <T> ResultData<T> error(String  msg,T data){
        return new ResultData<T>(msg,data);
    }

    public static <T> ResultData<T> error(Integer retCode,String  msg,T data){
        ResultData<T> resultData = new ResultData<>(msg, data);
        resultData.retCode = retCode;
        return resultData;
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static String errorJson(String msg){
        return gson.toJson(new ResultData(-1,msg));
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static String errorJsonWithCode(int retCode,String msg){
        return gson.toJson(new ResultData(retCode,msg));
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultData<T> error(CodeMsg cm,String msg){
        cm.setMessage(cm.getMessage()+"；"+msg);
        return new ResultData<T>(cm);
    }

    public static <T> ResultData<T> error(int code,String message){
        return new ResultData<>(code,message);
    }

    public static <T> ResultData<T> error(String message){
        return new ResultData<>(-1,message);
    }

    public T getData() {
        return data;
    }
    public String getMessage() {
        return message;
    }
    public int getRetCode() {
        return retCode;
    }
}
